home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch20 / source / display4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  4.1 KB  |  202 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <graphics.h>
  5. #include <io.h>
  6. #include <malloc.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10.  
  11.  
  12. void main(int argc, char **argv)
  13. {
  14. int i,j,k,l;
  15. FILE *datafile;
  16. unsigned char LUT[256][3];
  17. float temp;
  18. unsigned char *buffer;
  19. int filesize;
  20. int numslices;
  21. int grdriver, grmode, grerror;
  22. int tw;
  23. char title[20];
  24. char disp[128][128];
  25.  
  26.  
  27.   /* Ensure proper syntax */
  28.   
  29.   if(argc != 2)
  30.     {
  31.     printf("Usage::  Display4 datafile.\n\n");
  32.     exit(1);
  33.     }
  34.  
  35.   
  36.   /* open input files */
  37.   
  38.   datafile = fopen(argv[1], "rb");
  39.   if(datafile == NULL)
  40.     {
  41.     printf("Can not open file: %s.\n\n", argv[1]);
  42.     exit(2);
  43.     }
  44.  
  45.  
  46.   /* Create the LUT structure */
  47.  
  48.   for(i=0; i<64; i++)    /* ramp window fxn */
  49.     for(j=0; j<3; j++)
  50.       { 
  51.       LUT[i][j] = (unsigned char)i;
  52.       }
  53.  
  54.   for(i=64; i<128; i++)  /* quadratic window fxn */
  55.     for(j=0; j<3; j++)
  56.       {
  57.       temp = (float)(i-64)/63.0 * 7.9;
  58.       LUT[i][j] = (unsigned char)temp*temp;
  59.       }
  60.   
  61.   for(i=128; i<192; i++)  /* sqrt window fxn */
  62.     for(j=0; j<3; j++)
  63.       {
  64.       temp = (float)(i-128)/63.0 * 3968.0;
  65.       LUT[i][j] = (unsigned char)sqrt((double)temp);
  66.       }
  67.  
  68.   for(i=192; i<256; i++)  /* inverse ramp window fxn */
  69.     for(j=0; j<3; j++)
  70.       {
  71.       LUT[i][j] = (unsigned char)(63 - (i-192));
  72.       }
  73.  
  74.  
  75.   /* Determine the number of slices in the volume */
  76.  
  77.   filesize = filelength( fileno(datafile) );
  78.   numslices = filesize / 256 / 256;
  79.  
  80.   
  81.   /* Allocate memory for the disk buffer. */
  82.  
  83.   buffer = (unsigned char *)malloc( 256*256 );
  84.   if(buffer == NULL)
  85.     {
  86.     printf("Error allocating memory for buffer.\n\n");
  87.     exit(3);
  88.     }
  89.   
  90.   
  91.   /* check graphics hardware */
  92.  
  93.   grdriver = DETECTX;
  94.   grmode = 0;
  95.   detectgraph(&grdriver, &grmode);
  96.   switch(grdriver)
  97.     {
  98.     case VESA256:
  99.     case ATI256:
  100.     case COMPAQ:
  101.     case TSENG3256:
  102.     case TSENG4256:
  103.     case GENOA5:
  104.     case GENOA6:
  105.     case OAK:
  106.     case PARADIS256:
  107.     case TECMAR:
  108.     case TRIDENT256:
  109.     case VIDEO7:
  110.     case VIDEO7II:
  111.       break;
  112.  
  113.     default:
  114.       printf("Unable to detect 256 color graphics adapter.\n");
  115.       exit(4);
  116.       break;
  117.     }
  118.   
  119.   
  120.   grmode = 1;
  121.   initgraph(&grdriver, &grmode, "");
  122.   grerror = graphresult();
  123.   if(grerror)
  124.     {
  125.     closegraph();
  126.     printf("Error %d initializing graphics mode.\n", grerror);
  127.     exit(5);
  128.     }
  129.  
  130.   
  131.   /* Program the hardware palette */
  132.   
  133.   for(i=0; i<256; i++)
  134.     setrgbpalette(i, LUT[i][0], LUT[i][1], LUT[i][2]);
  135.   
  136.   
  137.   /* Display the image titles */
  138.  
  139.   setcolor(63);
  140.   
  141.   strcpy(title, "RAMP FUNCTION");
  142.   tw = textwidth(title);
  143.   outtextxy(160 - tw/2, 160, title);
  144.  
  145.   strcpy(title, "QUADRATIC FUNCTION");
  146.   tw = textwidth(title);
  147.   outtextxy(480 - tw/2, 160, title);
  148.   
  149.   strcpy(title, "SQUARE ROOT FUNCTION");
  150.   tw = textwidth(title);
  151.   outtextxy(160 - tw/2, 360, title);
  152.   
  153.   strcpy(title, "INVERSE RAMP FUNCTION");
  154.   tw = textwidth(title);
  155.   outtextxy(480 - tw/2, 360, title);
  156.  
  157.  
  158.   while(!kbhit())
  159.     {
  160.     for(i=0; i<numslices; i++)
  161.       {
  162.       
  163.       fseek(datafile, (long)i*256*256, SEEK_SET);
  164.       fread(buffer, 256*256, 1, datafile);
  165.  
  166.       for(j=0; j<128; j++)
  167.         for(k=0; k<128; k++) 
  168.           {
  169.           l =  (int)buffer[2*j*256 + 2*k];
  170.           l += (int)buffer[(2*j+1)*256 + 2*k]; 
  171.           l += (int)buffer[2*j*256 + 2*k+1];
  172.           l += (int)buffer[(2*j+1)*256 + 2*k+1];
  173.           l /= 16;
  174.           
  175.           disp[k][j] = l;
  176.           }
  177.  
  178.       for(j=0; j<128; j++)
  179.         for(k=0; k<128; k++) 
  180.           putpixel(96+k, 20+j, disp[k][j]);
  181.  
  182.       for(j=0; j<128; j++)
  183.         for(k=0; k<128; k++) 
  184.           putpixel(416+k, 20+j, disp[k][j] | 0x40);
  185.  
  186.       for(j=0; j<128; j++)
  187.         for(k=0; k<128; k++) 
  188.           putpixel(96+k, 220+j, disp[k][j] | 0x80);
  189.  
  190.       for(j=0; j<128; j++)
  191.         for(k=0; k<128; k++) 
  192.           putpixel(416+k, 220+j, disp[k][j] | 0xC0);
  193.       
  194.       }
  195.     }
  196.  
  197.   fclose(datafile);
  198.   free( (void *)buffer);
  199.   getch();
  200.   closegraph();
  201. }
  202.